package socket.udp.c16;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @author 周宇
 * @create 2020-05-26 12:44
 */
public class MesReceiver {
    public static void main(String[] args) throws IOException {
        // 定义一个接收端，并且指定了接收的端口号
        DatagramSocket socket = new DatagramSocket(1883, InetAddress.getByName("192.168.2.118"));
        while (true) {
            byte[] buf = new byte[48];
            // 解析数据包
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            socket.receive(packet);
            String ip = packet.getAddress().getHostAddress();
            buf = packet.getData();
            String hex = HexConvert.BinaryToHexString(buf);
            byte[] bytes = HexConvert.hexStringToBytes(hex);
            System.out.println("收到 " + ip + " 发来的消息：" + HexConvert.BinaryToHexString(bytes));
        }
    }
}